﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace Quan_ly_du_an
{
    public partial class frmDangkylopnangkhieu : Form
    {
        SqlConnection conn;
        SqlCommand cmd;
        //SqlCommand cmd1;
        SqlDataAdapter da;
        DataSet ds;
        SqlCommandBuilder cmb;
        public frmDangkylopnangkhieu()
        {
            String conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
            conn = new SqlConnection(conStr);
            conn.ConnectionString = conStr;
            InitializeComponent();
        }

        private void frmDangkylopnangkhieu_Load(object sender, EventArgs e)
        {
                conn.Open();
                cmd = new SqlCommand("select count(manhom) from Nhom;", conn);
                SqlDataReader reader = cmd.ExecuteReader();
                reader.Read();
                label4.Text = reader.GetValue(0).ToString();
                conn.Close();
                conn.Open();
                cmd = new SqlCommand("SELECT * FROM lopnangkhieu", conn);
                da = new SqlDataAdapter(cmd);
                ds = new DataSet();
                da.Fill(ds, "lopnangkhieu");
                cmb = new SqlCommandBuilder(da);
                dataGridView1.DataSource = ds.Tables["lopnangkhieu"];
                cmd = new SqlCommand("select malopNk from lopnangkhieu;", conn);
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "malopnk");
                comboBox1.DataSource = ds.Tables[0];
                comboBox1.DisplayMember = ds.Tables[0].Columns[0].ToString();
                comboBox1.ValueMember = ds.Tables[0].Columns[0].ToString();
                conn.Close();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            conn.Open();
            cmd = new SqlCommand("select count(stt) from Dklop where malopNk='"+comboBox1.SelectedValue.ToString()+"';", conn);
            SqlDataReader reader2 = cmd.ExecuteReader();
            reader2.Read();
            label4.Text = reader2.GetValue(0).ToString();
            int int_value = Convert.ToInt32(label4.Text);
            
            try
            {
                if ( txtMatre.Text == "")
                {
                    MessageBox.Show("Bạn chưa điền đầy đủ thông tin", "Có lỗi xảy ra!");
                    txtMatre.Focus();
                    return;
                }
                else if(int_value>30)
                {
                    MessageBox.Show("Lớp muốn đăng ký đã full , xin vui lòng chọn lớp khác để đăng ký", "Có lỗi xảy ra!");
                    txtMatre.Focus();
                    return;
                }
                else
                {
                    conn.Open();
                    cmd = new SqlCommand("SELECT * FROM DKlop", conn);
                    da = new SqlDataAdapter(cmd);
                    ds = new DataSet();
                    da.Fill(ds, "DKlop");
                    cmb = new SqlCommandBuilder(da);
                    conn.Close();
                    DataTable dt = ds.Tables[0];
                    DataRow newRow = dt.NewRow();
                    newRow["MalopNK"] = comboBox1.SelectedValue.ToString();
                    newRow["Matre"] = txtMatre.Text;
                    dt.Rows.Add(newRow);
                    da.Update(ds, "DKlop");
                    MessageBox.Show("Thêm thành công");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Lớp năng khiếu hoăc trẻ chưa có");
                frmQuanlysotre objfrmQuanlysotre = new frmQuanlysotre();
                this.Dispose();
                objfrmQuanlysotre.ShowDialog();
            } 
        }

       
        


        private void button4_Click(object sender, EventArgs e)
        {
            frmMenu objfrmMenu = new frmMenu();
            this.Dispose();
            objfrmMenu.ShowDialog();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát hay không  ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                this.Close();
            }
        }
    }
}
